package com.yict.csms.system.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.entity.PageEntity;
import com.yict.common.service.impl.BaseServiceImpl;
import com.yict.common.util.ObjectUtil;
import com.yict.csms.system.dao.IStationDao;
import com.yict.csms.system.entity.Profession;
import com.yict.csms.system.entity.Station;
import com.yict.csms.system.service.IStationService;

@Service("stationService")
public class StationServiceImpl extends
		BaseServiceImpl<Station, Long, IStationDao> implements IStationService {

	/**
	 * 查询列表
	 */
	public List<Station> list(Map<String, Object> queryMap, PageEntity page,
			LinkedHashMap<String, Boolean> orderList) {
		if(page == null) page = new PageEntity();
		List<Station> list = new ArrayList<Station>();
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuilder hql = new StringBuilder("from Station t where 1=1 ");
		String station = (String) queryMap.get("station");
		if (station != null && station.length() > 0) {
			map.put("station", "%" + station.toUpperCase() + "%");
			hql.append(" and upper(t.station) like :station");
		}
		String profename = (String) queryMap.get("profeName");
		if (profename != null && profename.length() > 0) {
			map.put("profename", "%" + profename.toUpperCase() + "%");
			hql.append(" and upper(t.profession.profename) like :profename");
		}
		Long profeid = ObjectUtil.s2l( (String)queryMap.get("profession.profeid"));
		if (profeid != null && profeid > 0) {
			map.put("profeid", profeid);
			hql.append(" and t.profession.profeid = :profeid");
		}
		int count = this.getBaseDao().queryCount(
				"select count(*) " + hql.toString(), map);
		page.setTotalRecord(count);
		if (count > 0) {
			if (orderList == null) {
				orderList = new LinkedHashMap<String, Boolean>();
			}
			if (orderList.size() == 0)
				orderList.put("updatedate", false);
			StringBuilder orderString = new StringBuilder("");
			for (Map.Entry<String, Boolean> entry : orderList.entrySet()) {
				String key = entry.getKey();
				boolean value = entry.getValue();
				if (orderString.length() > 0)
					orderString.append(",");
				orderString.append(key);
				if (!value)
					orderString.append(" desc");
			}
			hql.append(" order by ").append(orderString);
			list = this.getBaseDao().list(hql.toString(), map,
					(page.getToPage() - 1) * page.getPageSize(),
					page.getPageSize());
		}
		return list;
	}
	
	/**
	 * 根据id列表查询实体
	 * @param ids
	 * @return
	 */
	public List<Station> listByIds(Collection<Long> ids){
		if(ids == null || ids.size() == 0) return new ArrayList<Station>();
		String hql = "from Station t where stationid in (:ids)";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("ids", ids);
		return getBaseDao().list(hql,map, 0, 0);
	}

	@Override
	@Transactional(readOnly = false)
	public boolean update(Station t) throws Exception {
		Station vo = this.findById(t.getStationid());
		if(vo == null) throw new Exception("数据不存在，可能已经被删除");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("station", t.getStation());
		
		List<Station> list = super.list(map, null, null);
		if(list.size() > 0){
			Station po = list.get(0);
			if(list.size() > 1 || !vo.getStationid().equals(po.getStationid()))
				throw new Exception("同名的岗位已经存在");;
		}
		vo.setStation(t.getStation());
		vo.setProfession(t.getProfession());
		return true;
	}

	@Override
	@Transactional(readOnly = false)
	public boolean save(Station t) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("station", t.getStation());
		
		List<Station> list = super.list(map, null, null);
		if(list.size() > 0)
			throw new Exception("同名的岗位已经存在");
		return super.save(t);
	}

	@Override
	@Transactional(readOnly = false)
	public boolean remove(Long[] ids) throws Exception {
		try {
			super.remove(ids);
		} catch (Exception e) {
			throw new Exception("数据可能已经被其他数据引用");
		}
		return true;
	}

	@Resource(name = "stationDao")
	public void setBaseDao(IStationDao baseDao) {
		super.setBaseDao(baseDao);
	}
}
